/**
 * 条件：省市县地址的三级联动
 * @returns
 */
$(function(){
console.log("页面加载完成事件");
	//省市县2
	$.ajax({
		url:"DataTable/findAddress",
		type:"post",
		dataType:"json",
		success:function(map){
//console.log(map);
			var citylist=map.cityList;
			var countylist=map.countyList;
			var provincelist=map.provinceList;
//console.log(citylist);
//console.log(townlist);
//console.log(provincelist);
			var province_select = document.getElementById("province3");
			var city_select = document.getElementById("city3");
			var county_select = document.getElementById("county3");
			//消除下拉框
			province_select.innerHTML = "<option value='0'>选择省</option>";
			city_select.innerHTML = "<option value='0'>选择市</option>";
			county_select.innerHTML = "<option value='0'>选择县（区）</option>"
			//加载省份
			for(var i=0;i<provincelist.length;i++){
				var opt=document.createElement("option");
				opt.innerText=provincelist[i].name;
				opt.setAttribute("value",provincelist[i].code);
				province_select.appendChild(opt);
			}
			//给省份添加change事件，选择省份，获取城市
		    province_select.onchange = function(){
		        //获取省份ID
		        var provinceCode = province_select.value;
		        //先清除原来的城市数据
		        city_select.innerHTML = "<option value='0'>选择市</option>";
		        county_select.innerHTML = "<option value='0'>选择区</option>";
		        //根据省份code获取 城市列表(遍历省份去匹配)
		        for(var i=0;i<citylist.length;i++){
		            if(citylist[i].provincecode == provinceCode){
		                var citys = citylist[i].name;
		                var opt = document.createElement("option");
		                opt.innerText = citylist[i].name;
		                opt.setAttribute("value", citylist[i].code);
		                city_select.appendChild(opt);
		            }
		         }
		   }
		   //给城市添加change事件，选择城市，获取区县
		   city_select.onchange = function(){
		        //获取城市ID
		        var cityCode = city_select.value;
		        //先清除原来的城市数据
		        county_select.innerHTML = "<option value='0'>选择区</option>";
		        //根据城市ID获取区县列表
		        for(var i=0;i<countylist.length;i++){
		            if(countylist[i].citycode == cityCode){
		                var opt = document.createElement("option");
		                opt.innerText =countylist[i].name;
		                opt.setAttribute("value", countylist[i].code);
		                county_select.appendChild(opt);
		            }
		        }
		    }
		},
		error:function(){
			console.log("数据返回失败");
		}		
	});
	
	
	
})